package com.wgchao.pharmacymg.domain.dao;

import com.wgchao.pharmacymg.Infrastructure.exception.define.ObjectNotExistedException;
import com.wgchao.pharmacymg.Infrastructure.exception.define.ParamMissexception;
import com.wgchao.pharmacymg.Infrastructure.repository.PhDrugStorageRepository;
import com.wgchao.pharmacymg.domain.entity.PhDrugStorageDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @ClassName: PhDrugStorageDao
 * @author: wgchao
 * @createTime: 2019/11/17 9:37 AM
 * @Description: TODO
 */
@Service
public class PhDrugStorageDao {
    @Autowired
    private PhDrugStorageRepository drugStorageRepository;

    public PhDrugStorageDO findByCode(String code){
        Optional<PhDrugStorageDO> drugStorageDOOptional = drugStorageRepository.findById(code);
        if(!drugStorageDOOptional.isPresent()){
            throw new ObjectNotExistedException("药库不存在");
        }
        return drugStorageDOOptional.get();
    }

    public PhDrugStorageDO save(PhDrugStorageDO drugStorage){
        drugStorage = drugStorageRepository.save(drugStorage);
        return drugStorage;
    }

    public List<PhDrugStorageDO> qbc(String storageCode, String agencyCode, String storageName, Long dsl){
        if(StringUtils.isEmpty(agencyCode)){
            throw new ParamMissexception("机构编号缺失");
        }
        Specification<PhDrugStorageDO> specification = new Specification<PhDrugStorageDO>() {
            @Override
            public Predicate toPredicate(Root<PhDrugStorageDO> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<>();
                Predicate predicate1 = criteriaBuilder.equal(root.get("agencyCode"), agencyCode);
                predicateList.add(predicate1);
                if(StringUtils.isEmpty(storageCode)){
                    Predicate predicate = criteriaBuilder.equal(root.get("storageCode"), storageCode);
                    predicateList.add(predicate);
                }
                if(StringUtils.isEmpty(storageName)){
                    Predicate predicate = criteriaBuilder.like(root.get("storageName"), storageName+"%");
                    predicateList.add(predicate);
                }
                if(dsl != null){
                    Predicate predicate = criteriaBuilder.equal(root.get("dsl"), dsl);
                    predicateList.add(predicate);
                }

                Predicate[] predicates = new Predicate[predicateList.size()];
                return criteriaBuilder.and(predicateList.toArray(predicates));
            }
        };
        Sort sort = Sort.by(Sort.Order.asc("storageCode"));
        return drugStorageRepository.findAll(specification, sort);
    }
}
